#!/bin/bash

############################################
##
##    Menu Order Fix Install Script
##
##    Created by: h3lbr0 // atvMod.com
##
############################################

function applyMenuOrder {

INFO_DIR=$1
ORDER=$2

if [ -f $INFO_DIR/Info.plist ]; then

cp $INFO_DIR/Info.plist $INFO_DIR/Info.plist.bak
sed '/^$/d' $INFO_DIR/Info.plist > $INFO_DIR/Info1.plist
sed '/FRAppliancePreferedOrderValue/{N;d;}' $INFO_DIR/Info1.plist > $INFO_DIR/Info2.plist
sed 'N;$!P;$!D;$d' $INFO_DIR/Info2.plist > $INFO_DIR/Info3.plist
echo -e "\t<key>FRAppliancePreferedOrderValue</key>\n\t<real>$ORDER</real>\n</dict>\n</plist>" >> $INFO_DIR/Info3.plist

rm $INFO_DIR/Info{1,2}.plist
mv $INFO_DIR/Info3.plist $INFO_DIR/Info.plist

fi

unset ORDER
unset INFO_DIR

}

## Menu Order
PLUGIN_DIR="/System/Library/CoreServices/Finder.app/Contents/PlugIns"

# Movies
# <real>0.0</real>
MOVIES_INFO_DIR="$PLUGIN_DIR/Movies.frappliance/Contents"
MOIVES_ORDER=1
applyMenuOrder $MOVIES_INFO_DIR $MOIVES_ORDER
unset MOVIES_INFO_DIR
unset MOIVES_ORDER

# TV Shows
# <real>1</real>
TVSHOWS_INFO_DIR="$PLUGIN_DIR/TV.frappliance/Contents"
TVSHOWS_ORDER=2
applyMenuOrder $TVSHOWS_INFO_DIR $TVSHOWS_ORDER
unset TVSHOWS_INFO_DIR
unset TVSHOWS_ORDER

# Music
# <real>3</real>
MUSIC_INFO_DIR="$PLUGIN_DIR/Music.frappliance/Contents"
MUSIC_ORDER=3
applyMenuOrder $MUSIC_INFO_DIR $MUSIC_ORDER
unset MUSIC_INFO_DIR
unset MUSIC_ORDER

# Podcasts
# <real>4</real>
PODCASTS_INFO_DIR="$PLUGIN_DIR/Podcasts.frappliance/Contents"
PODCASTS_ORDER=4
applyMenuOrder $PODCASTS_INFO_DIR $PODCASTS_ORDER
unset PODCASTS_INFO_DIR
unset PODCASTS_ORDER

# Photos
# <real>5</real>
PHOTOS_INFO_DIR="$PLUGIN_DIR/Photos.frappliance/Contents"
PHOTOS_ORDER=5
applyMenuOrder $PHOTOS_INFO_DIR $PHOTOS_ORDER
unset PHOTOS_INFO_DIR
unset PHOTOS_ORDER

# YouTube
# <real>6</real>
YOUTUBE_INFO_DIR="$PLUGIN_DIR/YT.frappliance/Contents"
YOUTUBE_ORDER=6
applyMenuOrder $YOUTUBE_INFO_DIR $YOUTUBE_ORDER
unset YOUTUBE_INFO_DIR
unset YOUTUBE_ORDER

# Files
# <real>2.8999999999999999</real>
ATVFILES_INFO_DIR="$PLUGIN_DIR/ATVFiles.frappliance/Contents"
ATVFILES_ORDER=7
applyMenuOrder $ATVFILES_INFO_DIR $ATVFILES_ORDER
unset ATVFILES_INFO_DIR
unset ATVFILES_ORDER

# nitoTV
# <real>2.0019999999999998</real>
NITOTV_INFO_DIR="$PLUGIN_DIR/nitoTV.frappliance/Contents"
NITOTV_ORDER=8
applyMenuOrder $NITOTV_INFO_DIR $NITOTV_ORDER
unset NITOTV_INFO_DIR
unset NITOTV_ORDER

# Sapphire
# <real>0.0</real>
SAPPHIRE_INFO_DIR="$PLUGIN_DIR/Sapphire.frappliance/Contents"
SAPPHIRE_ORDER=9
applyMenuOrder $SAPPHIRE_INFO_DIR $SAPPHIRE_ORDER
unset SAPPHIRE_INFO_DIR
unset SAPPHIRE_ORDER

# Couch Surfer
# <real>2.0019999999999998</real>
COUCHSURFER_INFO_DIR="$PLUGIN_DIR/CouchSurfer.frappliance/Contents"
COUCHSURFER_ORDER=10
applyMenuOrder $COUCHSURFER_INFO_DIR $COUCHSURFER_ORDER
unset COUCHSURFER_INFO_DIR
unset COUCHSURFER_ORDER

# Maintenance
# <real>10</real>
MAINTENANCE_INFO_DIR="$PLUGIN_DIR/scripts.frappliance/Contents"
MAINTENANCE_ORDER=11
applyMenuOrder $MAINTENANCE_INFO_DIR $MAINTENANCE_ORDER
unset MAINTENANCE_INFO_DIR
unset MAINTENANCE_ORDER

# Settings
# <real>7</real>
SETTINGS_INFO_DIR="$PLUGIN_DIR/Settings.frappliance/Contents"
SETTINGS_ORDER=12
applyMenuOrder $SETTINGS_INFO_DIR $SETTINGS_ORDER
unset SETTINGS_INFO_DIR
unset SETTINGS_ORDER
